import { deepClone } from '@/utils/index'
const componentChild = {}

const slotsFiles = require.context('./slots', false, /\.js$/)
const keys = slotsFiles.keys() || []
keys.forEach(key => {
  const tag = key.replace(/^\.\/(.*)\.\w+$/, '$1')
  const value = slotsFiles(key).default
  componentChild[tag] = value
})

function vModel(dataObject, defaultValue) {
    dataObject.props.value = defaultValue;
    dataObject.on.input = val => {
        this.$emit('input',val)
    }
    console.log(defaultValue);
    
}
// 标签属性 
function makeDataObject() {
    return {
      class: {},
      attrs: {},
      props: {},
      domProps: {},
      nativeOn: {},
      on: {},
      style: {},
      directives: [],
      scopedSlots: {},
      slot: null,
      key: null,
      ref: null,
      refInFor: true
    }
}

function mountSlotFiles(h, confClone, children) {
    const childObjs = componentChild[confClone.__config__.tag]
    if (childObjs) {
      Object.keys(childObjs).forEach(key => {
        const childFunc = childObjs[key]
        if (confClone.__slot__ && confClone.__slot__[key]) {
          children.push(childFunc(h, confClone, key))
        }
      })
    }
  }

  function emitEvents(confClone) {
    ['on', 'nativeOn'].forEach(attr => {
      const eventKeyList = Object.keys(confClone[attr] || {})
      eventKeyList.forEach(key => {
        const val = confClone[attr][key]
        if (typeof val === 'string') {
          confClone[attr][key] = event => this.$emit(val, event)
        }
      })
    })
  }

  function clearAttrs(dataObject) {
    delete dataObject.attrs.__config__
    delete dataObject.attrs.__slot__
    delete dataObject.attrs.__methods__
  }

  function buildDataObject(confClone, dataObject) {
    Object.keys(confClone).forEach(key => {
      const val = confClone[key]
      console.log('key,,,',key);
      
      vModel.call(this, dataObject, confClone.__config__.defaultValue)
      // if (key === '__vModel__') {
      //   vModel.call(this, dataObject, confClone.__config__.defaultValue)
      // } else if (dataObject[key] !== undefined) {
      //   if (dataObject[key] === null
      //     || dataObject[key] instanceof RegExp
      //     || ['boolean', 'string', 'number', 'function'].includes(typeof dataObject[key])) {
      //     dataObject[key] = val
      //   } else if (Array.isArray(dataObject[key])) {
      //     debugger
      //     dataObject[key] = [...dataObject[key], ...val]
      //   } else {
      //     dataObject[key] = { ...dataObject[key], ...val }
      //   }
      // } else {
      //   dataObject.attrs[key] = val
      // }
    })
  
    // 清理属性
    clearAttrs(dataObject)
  }

export default {
    props: {
        // 传入item
        conf: {
            type: Object,
            required: true
        }
    },
    render(h) {
        const dataObject = makeDataObject()
        const confClone = deepClone(this.conf)
        const children = this.$slots.default || []
        mountSlotFiles.call(this, h, confClone, children)

        emitEvents.call(this, confClone)

        buildDataObject.call(this, confClone, dataObject)

        return h(this.conf.__config__.tag,dataObject,children)
    }
}